﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GNF.GNFService.GNFServer;
using GNF.Geo;
using System.Threading;

namespace GNF.GNFService.GNFServer
{
    /// <summary>
    /// a GNF Service emulator that sends the data as the current time, the data is
    /// sent in the following format:
    /// generate a new GPS Data based on the current time
    /// longitude - current hour
    /// latitude - current minute
    /// altitude - current second
    /// Sat - current milisecond
    /// </summary>
    public class RandomDataGNFServer : GNFServiceServer
    {

        #region private memebrs

        /// <summary>
        /// is the server running
        /// </summary>
        public bool _isRunning;

        /// <summary>
        /// is the server is init
        /// </summary>
        public bool _isInit;

        /// <summary>
        /// the thread the server runs on
        /// </summary>
        private Thread _RunThread;

        #endregion

        #region Data generetors

        /// <summary>
        /// generate a new GPS Data based on the current time
        /// longitude - current hour
        /// latitude - current minute
        /// altitude - current second
        /// Sat - current milisecond
        /// </summary>
        /// <returns>the GPS Data generated</returns>
        public static GPSData GenerateTimeData()
        {
            DateTime now = DateTime.Now;
            return new GPSData(now.Hour, now.Minute, now.Second, now.Millisecond);
        }

        /// <summary>
        /// generate a random data (each value is between 1-100,
        /// </summary>
        /// <returns>return the generated data</returns>
        public static GPSData GenerateRandomData()
        {
            Random r = new Random();
            return new GPSData(r.Next(100), r.Next(100), r.Next(100), r.Next(1, 10));
        }

        #endregion

        /// <summary>
        /// start the server, and start the server that pull the generated data
        /// </summary>
        public void Start()
        {
            
             Connect();
             _RunThread = new Thread(new ThreadStart(StartSendingInformation));
            

            _isRunning = true;

            _RunThread.Start();

         
        }

        /// <summary>
        /// start emulating data
        /// </summary>
        public void StartSendingInformation()
        {
            while (_isRunning)
            {
                if (HasSubscribers)
                {
                    PublishGPSData(GenerateTimeData());
                    System.Threading.Thread.Sleep(100);
                }
                else
                {
                    System.Threading.Thread.Sleep(500);
                }

            }
        }

        /// <summary>
        /// close the connnection to the server
        /// </summary>
        public void Close()
        {
            _isRunning = false;
            _RunThread.Abort();
            CloseConnection();
        }

        

        

    }
}
